import 'package:flutter/material.dart';
import 'package:flutter_roujiaosuo/blocs/bloc_provider.dart';
import 'package:flutter_roujiaosuo/blocs/supply_bloc.dart';
import 'package:flutter_roujiaosuo/requests/models/SupplyModel.dart';
import 'package:flutter_roujiaosuo/ui/components/net_progress.dart';
import 'package:flutter_roujiaosuo/ui/components/refresh_listView.dart';
import 'package:flutter_roujiaosuo/ui/components/supply/supply_item.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class SupplyListView extends StatefulWidget {
  SupplyListView({Key key}) : super(key: key);

  _SupplyListViewState createState() => _SupplyListViewState();
}

class _SupplyListViewState extends State<SupplyListView> {
  final bloc = SupplyBloc();
  bool _showProgeress = true;
  int page = 1;
  List<SupplyModel> list = List<SupplyModel>();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    bloc.getData(1);
    bloc.supplyListStream.listen((item) {
      if (item != null) {
        setState(() {
          _showProgeress = false;
        });
      }
    });
  }

  void _refresh(bool isRefresh) {
    setState(() {
      _showProgeress = true;
    });
    if (isRefresh) {
      page = 1;
      list.clear();
    } else {
      page++;
    }
    bloc.getData(page);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      bloc: bloc,
      child: Scaffold(
        appBar: AppBar(
          title: Text('供应列表'),
        ),
        body: StreamBuilder(
          stream: bloc.supplyListStream,
          builder: (context, AsyncSnapshot<Supply> snaps) {
            if (snaps.data != null) {
              list.addAll(snaps.data.data);
            }
            return SafeArea(
              child: Stack(children: <Widget>[
                RefreshListView(
                    itemHeight: 110,
                    itemCount: list.length,
                    code: snaps.data == null ? null : snaps.data.code,
                    connectionState: snaps.connectionState,
                    refreshBlock: _refresh,
                    builder: (context, index) {
                      if (index == list.length) {
                        if (list.length % 10 == 0) {
                          return Container(
                            child: SpinKitCircle(
                              color: Theme.of(context).primaryColor,
                              size: 30,
                            ),
                          );
                        } else {
                          return Container(
                            height: 44,
                            alignment: Alignment.center,
                            child: Text("没有更多数据"),
                          );
                        }
                      }
                      return SupplyItemView(
                        itemClickBlock: (SupplyModel model) {
                          print('${model.title}');
                        },
                        height: 110,
                        model: list[index],
                      );
                    }),
                _showProgeress
                    ? RefreshProgressView()
                    : Container(
                        width: 0,
                        height: 0,
                      ),
              ]),
            );
          },
        ),
      ),
    );
  }
}
